package cn.meomis.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;

import org.apache.struts2.dispatcher.SessionMap;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;

import cn.meomis.common.util.DataTableParam;
import cn.meomis.common.util.VOSet;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class BaseAction extends ActionSupport implements SessionAware,
		ServletRequestAware, ServletResponseAware {

	private static final long serialVersionUID = 7620009925942346125L;

	ActionContext context = ActionContext.getContext();
	HttpServletRequest httpServletRequest;
	HttpServletResponse httpServletResponse;
	@SuppressWarnings("rawtypes")
	SessionMap session;

	@SuppressWarnings("rawtypes")
	public void setSession(Map map) {
		this.session = (SessionMap) map;
	}

	public void setServletRequest(HttpServletRequest request) {
		this.httpServletRequest = request;
	}

	public void setServletResponse(HttpServletResponse response) {
		this.httpServletResponse = response;
	}

	public DataTableParam getParam() throws JSONException {

		DataTableParam param = new DataTableParam();
		String isQuery = httpServletRequest.getParameter("isQuery");
		param.setIsQuery(isQuery);
		String vosetString = httpServletRequest.getParameter("voList");
		
		JSONArray jsonArray = new JSONArray("["+vosetString+"]");

		List<VOSet> voList = new ArrayList<VOSet>();
		for (int i = 0; i < jsonArray.length(); i++) {
			JSONObject jsonobject2 = jsonArray.getJSONObject(i);
			VOSet vs = new VOSet();
			vs.setKey(jsonobject2.getString("key"));
			vs.setValue(jsonobject2.getString("value"));
			voList.add(vs);
		}

		param.setVoList(voList);
		return param;
	}
	
	protected HashMap<String, String> convertToMap(List<VOSet> params) {
		HashMap<String, String> map = new HashMap<String, String>();
		for (VOSet param : params) {
			map.put(param.getKey(), param.getValue());
		}
		return map;
	} 

}
